package com.lechi.web.controller;

import com.lechi.web.common.annotation.Log;
import com.lechi.web.common.core.controller.BaseController;
import com.lechi.web.common.core.domain.AjaxResult;
import com.lechi.web.common.enums.BusinessType;
import com.lechi.web.constants.Constants;
import com.lechi.web.service.IIotDeviceService;
import com.lechi.web.system.domain.IotDeviceBo;
import com.lechi.web.system.domain.IotInterfaceDeviceBo;
import com.lechi.web.system.service.IIotInterfaceDeviceService;
import com.lechi.web.vo.IotInterfaceDeviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 通用接口和设备关系Controller
 *
 * @author yangguang
 * @date 2025-03-31
 */
@RestController
@RequestMapping("/iot/interface/device")
public class IotInterfaceDeviceController extends BaseController {
    @Autowired
    private IIotInterfaceDeviceService iotInterfaceDeviceService;

    @Autowired
    private IIotDeviceService iotDeviceService;

    /**
     * 查询通用接口和设备关系列表
     */
    @GetMapping("/treeSelect/{interfaceId}")
    public AjaxResult interfaceMenuTreeSelect(@PathVariable("interfaceId") Long interfaceId) {
        IotDeviceBo iotDeviceBo = new IotDeviceBo();
        iotDeviceBo.setProtocolType(String.format("'%s'", String.join("','", Constants.MODBUS_RTU, Constants.WEATHER2017, Constants.WEATHER2024)));
        List<IotDeviceBo> iotDeviceBoList = iotDeviceService.selectIotDeviceList(iotDeviceBo);
        final Map<String, List<IotDeviceBo>> collect = iotDeviceBoList.stream().collect(Collectors.groupingBy(IotDeviceBo::getGroupName));
        AtomicInteger num = new AtomicInteger(1);
        final List<Map<String, Object>> list = collect.entrySet().stream().map(entry -> {
            Map<String, Object> map = new HashMap<>();
            map.put("deviceGroupId", num.getAndIncrement());
            map.put("deviceGroupName", entry.getKey());
            map.put("deviceData", entry.getValue());
            return map;
        }).toList();
        AjaxResult ajax = AjaxResult.success();
        IotInterfaceDeviceBo iotInterfaceDevice = new IotInterfaceDeviceBo();
        iotInterfaceDevice.setInterfaceId(interfaceId);
        ajax.put("checkedKeys", iotInterfaceDeviceService.selectIotInterfaceDeviceList(iotInterfaceDevice));
        ajax.put("devices", list);
        return ajax;
    }
    /**
     * 新增通用接口和设备关系
     */
    @Log(title = "通用接口和设备关系", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody IotInterfaceDeviceVo iotInterfaceDevice) {
        iotInterfaceDeviceService.deleteIotInterfaceDeviceByInterfaceId(iotInterfaceDevice.getInterfaceId());
        final List<IotInterfaceDeviceBo> list = Arrays.stream(iotInterfaceDevice.getDeviceIds()).map(deviceId -> {
            IotInterfaceDeviceBo iotInterfaceDeviceBo = new IotInterfaceDeviceBo();
            iotInterfaceDeviceBo.setInterfaceId(iotInterfaceDevice.getInterfaceId());
            iotInterfaceDeviceBo.setDeviceId(deviceId);
            return iotInterfaceDeviceBo;
        }).toList();
        if (list.isEmpty()) {
            return AjaxResult.success();
        }
        iotInterfaceDeviceService.insertIotInterfaceDevice(list);
        return AjaxResult.success();
    }

}
